package com.like.fragment;

import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ScrollView;
import android.widget.TextView;

import com.like.dialog.ImgDialogFragment;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.InfoDetailReq;
import com.like.entity.InfoDetailResp;
import com.like.entity.InfoListResp;
import com.like.hospitalworld.InfoDetailActivity;
import com.like.hospitalworld.R;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;
import com.like.utils.DateConverter;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.fragment_info_detail_content)
public class InfoDetailContentFragment extends BaseFragment {

    @ViewInject(R.id.title)
    private TextView mTitle;
    @ViewInject(R.id.time)
    private TextView mTime;
    @ViewInject(R.id.content_webview)
    private WebView mContentWebView;
    @ViewInject(R.id.scrollview)
    private ScrollView mScrollview;

    private InfoService mInfoService;
    private InfoListResp.InformationBase mInfo;
    private InfoDetailResp mInfoDetailResp;
    private ImgDialogFragment mImgDialog;

    @Override
    protected void onCreateView() {
        super.onCreateView();
        mInfoService = RetrofitUtil.getService(InfoService.class);
        mImgDialog = new ImgDialogFragment();
        mScrollview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                EventBus.getDefault().post(new EventWrapper(null, InfoDetailActivity.class, InfoDetailActivity.REQUEST_CLOSE_COMMENT));
                return false;
            }
        });
        initWebView();
        getInfoDetail();
    }

    private void addImgClickListener() {
        mContentWebView.loadUrl(Constants.IMG_CLICK_JS);
    }

    private void initWebView() {
        mContentWebView.getSettings().setJavaScriptEnabled(true);
        mContentWebView.addJavascriptInterface(new JavascriptInterface(), "imageListner");
        mContentWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                addImgClickListener();
            }
        });
    }

    public class JavascriptInterface {
        @android.webkit.JavascriptInterface
        public void openImage(String url) {
            mImgDialog.setImgUrl(url);
            mImgDialog.show(getChildFragmentManager(), "img_dialog");
        }
    }

    public void setInfoBase(InfoListResp.InformationBase infoBase) {
        this.mInfo = infoBase;
    }

    private void getInfoDetail() {
        InfoDetailReq req = new InfoDetailReq(mInfo.id, m.getLoginResp().userID);
        Call<GeneralResponse<InfoDetailResp>> call = mInfoService.getInfoDetail(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<InfoDetailResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<InfoDetailResp>> call, Response<GeneralResponse<InfoDetailResp>> response) {
                mInfoDetailResp = response.body().data;
                mTime.setText(DateConverter.convertShitToDate(mInfoDetailResp.updatetime, "yyyy-MM-dd HH:mm:ss"));
                mTitle.setText(mInfoDetailResp.title);
                EventBus.getDefault().post(new EventWrapper(mInfoDetailResp, InfoDetailActivity.class, InfoDetailActivity.REQUEST_INFO_DATA));
                mContentWebView.loadDataWithBaseURL(null, Constants.FONT_STYLE_START + mInfoDetailResp.textInfo + Constants.FONT_STYLE_END, "text/html", "utf-8", null);
            }

            @Override
            public void onFailure(Call<GeneralResponse<InfoDetailResp>> call, Throwable t) {
            }
        });
    }

}
